﻿using SIMS.Shared.Models;
using SIMS.SqlSugarWebApi.Models;
using SIMS.SqlSugarWebApi.ServiceInterface;
using SqlSugar;

namespace SIMS.SqlSugarWebApi.Services
{
    public class MenuService : BaseService<Menu>, IMenuService
    {
        private DataContext _context;

        public MenuService(DataContext context) : base(context)
        {
            _context = context;
        }

        public async Task<PagingModel<Menu>> GetMenus(string menuName, int pageNum, int pageSize)
        {
            ISugarQueryable<Menu> menus;
            if (!string.IsNullOrEmpty(menuName))
            {
                menus = _context.db.Queryable<Menu>().Where(r => r.Name.Contains(menuName)).OrderBy(r => r.Id);
            }
            else
            {
                menus = _context.db.Queryable<Menu>().OrderBy(r => r.Id);
            }
            int count = await menus.CountAsync();
            List<Menu> items;
            if (pageSize > 0)
            {
                items = await menus.ToPageListAsync(pageNum, pageSize);
            }
            else
            {
                items = await menus.ToListAsync();
            }
            return new PagingModel<Menu>()
            {
                Count = count,
                Items = items
            };
        }

    }
}
